package org.gruposp2p.aula.gwt.client.representation;

import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.aula.gwt.client.util.LinkConstants;
import org.restlet.data.Status;
import org.restlet.gwt.Callback;
import org.restlet.gwt.Client;
import org.restlet.gwt.Context;
import org.restlet.gwt.data.Form;
import org.restlet.gwt.data.Method;
import org.restlet.gwt.data.Parameter;
import org.restlet.gwt.data.Protocol;
import org.restlet.gwt.data.Request;
import org.restlet.gwt.data.Response;
import com.google.gwt.core.client.GWT;
import com.google.gwt.xml.client.Document;

public class RepresentationService {

	private static RepresentationService instance;
	
	private RepresentationService ()  {
		loadInitialData();
		//selectedCourseListener = new ArrayList<RepresentationListener>();
	}
	
	public static RepresentationService getInstance () {
		if (instance == null) instance = new RepresentationService();  
		return instance;		
	}
	
	public static void initService () {
		getInstance();		
	}
	  
	  public void loadInitialData () {
		  Context context = new Context();
		  List<Parameter> lista = new ArrayList<Parameter>(); 
		  Parameter mediatype = new Parameter("Content-Type", "");
		  lista.add(mediatype);
		  context.setParameters(new Form(lista));
		  final Client client = new Client(Protocol.HTTP);
		  //client.setContext(context);
		  /*client.get(LinkConstants.getCoursesLink(), new Callback() {
			  @Override
			  public void onEvent(Request request, Response response) {
				  //GWT.log(response.getEntity().getText(), null);
            		//setXmlRepresentation(response.getEntityAsXml());
				  //Document document = response.getEntityAsXml().getDocument();
				  if (Status.SUCCESS_OK.equals(response.getStatus())) {
					  GWT.log("SUCCESS_OKSUCCESS_OKSUCCESS_OK: " + response.getStatus(), null);
					  //CoursesRepresentation.getInstance().setXmlRepresentation(response.getEntityAsXml());
				  } else {
					  GWT.log("ERRORERRORERRORERROR: " + response.getStatus(), null);
				  } 
			  }});  */
	  }	  
	
	  public void loadInitialData1 () {
	        Request request = new Request(Method.GET,
	        		LinkConstants.getCoursesLink());
	        // Indicates the client preferences and let the server handle
	        // the best representation with content negotiation.
	        new Client(Protocol.HTTP).handle(request, new Callback() {
			  @Override
			  public void onEvent(Request request, Response response) {
				  GWT.log(response.getEntityAsXml().getText(), null);
            		//setXmlRepresentation(response.getEntityAsXml());
				  Document document = response.getEntityAsXml().getDocument();
				  GWT.log(document.toString(), null);
				  CoursesRepresentation.getInstance().setXmlRepresentation(document);
			  }});  
	  }	 	  
	
	  public CoursesRepresentation getCoursesRepresentation () {
		  return CoursesRepresentation.getInstance();
	  }
	  
	  /*
	  public void addSelectedCourseListener(RepresentationListener listener) {
		  selectedCourseListener.add(listener);
	  }*/
}
